<?php
declare (strict_types = 1);

namespace app\command;

use app\model\zs\ZsBusiness;
use app\model\zs\ZsMemberCoupon;
use app\model\zs\ZsMemberCouponProvide;
use app\repository\BusinessRepository;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class BusinessCalcFloor extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('business:calc_floor')
            ->setDescription('批量填充business表的floor字段');
    }

    protected function execute(Input $input, Output $output)
    {
        $businessRepository = app(BusinessRepository::class);
        // 更新条数
        $num = 0;

        $list = ZsBusiness::where('loginid', '<>', '')->select();
        $list->each(function ($model_business) use (&$num, $businessRepository) {
            $floor_num = $businessRepository->resolveFloor($model_business->loginid);
            $curr_floor = intval($model_business->floor_new);
            if ($curr_floor === $floor_num) {
                return;
            }
            $model_business->floor_new = $floor_num;
            $model_business->save();
            ++$num;
        });

        // 指令输出
        $output->writeln("更新 {$num} 条");
    }
}
